package com.scraper.articles.web.controller;

import com.scraper.articles.web.model.SearchTerm;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/app")
public class AppController {

    @RequestMapping(value = {"/"})
    public String home(Map<String, Object> map) {

        map.put("msg", "Hello!");

        return "/app/index";
    }

    @RequestMapping(value = {"/search"})
    public
    @ResponseBody
    List<SearchTerm> search(@RequestParam("term") String term) {
        ArrayList<SearchTerm> searchTerms = new ArrayList<>();
        if (term.toLowerCase().startsWith("MS".toLowerCase())) {
            searchTerms.add(new SearchTerm("MSFT", "MSFT"));
        } else if (term.toLowerCase().startsWith("AA".toLowerCase())) {
            searchTerms.add(new SearchTerm("AAPL", "AAPL"));
        }
        return searchTerms;
    }
}
